import pymysql

from .conn_mysql import ConnectMysql

class ApiMysqlclass:

    def __init__(self):
        pass

    def get_return(self, sql, page, limit, party=None):

        con_obj = ConnectMysql()

        if page == None:
    
            page = '1'

        if limit == None:

            limit = '20'

        if page.isdecimal() and limit.isdecimal(): #检查参数合法性

            page = int(page)

            limit = int(limit)

        else:

            return {
                'status': -1,
                'errorCode': 'ValueError',
                'errorMsg':f'请检查page和limit参数是否合法,page:{page},limit:{limit}',
                'resultBody':None
            }

        if party == None:

            result_info = con_obj.select_all(sql, ((page-1)*limit, limit))
        
        else:

            # party = str(party)

            print(party)

            sql = "select * from company WHERE party=%s limit %s,%s"

            result_info = con_obj.select_all(sql, (party, (page-1)*limit, limit))

        con_obj.close()

        return {

            'status': 1,
            'errorCode': '',
            'errorMsg':'',
            'resultBody': result_info,
        }

    def get_return_id(self, sql, ID):

        con_obj = ConnectMysql()

        info = con_obj.select_all(sql, (ID,))

        sql = 'SELECT * FROM company WHERE id=%s;'
        consumption_surplus = con_obj.select_one(sql,(ID,))

        

        if not info and not consumption_surplus:
    
            con_obj.close()

            return {

                'status': -1,
                'errorCode': 'keyserror',
                'errorMsg':'无此ID对应的数据',
                'resultBody': None,
            }

        else:

            con_obj.close()

            return {

                'status': 1,
                'errorCode': '',
                'errorMsg':'',
                'resultBody': {'info':info, 'consumption_surplus':consumption_surplus['consumption_surplus']},
            }

    def get_company_id(self, sql, ID):#获取company指定id的信息
    
        con_obj = ConnectMysql()

        info = con_obj.select_all(sql, (ID,))

        if info == None:
    
            con_obj.close()

            return {

                'status': -1,
                'errorCode': 'keyserror',
                'errorMsg':'无此ID对应的数据',
                'resultBody': None,
            }

        else:

            con_obj.close()

            return {

                'status': 1,
                'errorCode': '',
                'errorMsg':'',
                'resultBody': info,
            }
    
    def post_return(self, sql, info):

        con_obj = ConnectMysql()
        print(info)
    
        if all(info):
    
            try:

                con_obj.insert_info(sql, info)

            except pymysql.err.DataError as e:

                print(e.args)

                return {

                    'status': -1,
                    'errorCode': e.args[0],
                    'errorMsg':'pymysql.err.DataError:' + e.args[1],
                    'resultBody': None,
                }

            else:

                return {

                    'status': 1,
                    'errorCode': '',
                    'errorMsg':'',
                    'resultBody': "入库成功",
                }

            finally:

                con_obj.close() 

        else:

            con_obj.close()

            return {

                'status': -1,
                'errorCode': 'valueserror',
                'errorMsg':'请检查提交的数据',
                'resultBody': None,
            }

    def put_return(self, sql, info):

        con_obj = ConnectMysql()

        if all(info):
        
            try:

                con_obj.update_info(sql, info)

            except pymysql.err.DataError as e:

                print(e.args)

                return {

                    'status': -1,
                    'errorCode': e.args[0],
                    'errorMsg':'pymysql.err.DataError:' + e.args[1],
                    'resultBody': None,
                }

            else:

                return {

                    'status': 1,
                    'errorCode': '',
                    'errorMsg':'',
                    'resultBody': "更改成功",
                } 

            finally:

                con_obj.close() 

        else:

            con_obj.close()

            return {

                'status': -1,
                'errorCode': 'valueserror',
                'errorMsg':'请检查提交的数据',
                'resultBody': None,
            }



